import * as echarts from '../../ec-canvas/echarts';

const app=getApp();
async function setOption(chart) {
  return await new Promise((resolve, reject) => {
    wx.request({
      url: "http://47.103.195.19/api/wechat/count/index",
      method: "get",
      header: {
        "content-type": "application/x-www-form-urlencoded",
      },
      success(response) {
        console.log(response);
        if (response.data.code == 200) {
          console.log("输出一下返回数据", response.data.objectResult.obj);
          console.log("输出一下每日数据",response.data.objectResult.obj.dailyCount);
          const option = {
            title: {
              text: "本周活跃人数",
            },
            legend: {
              right: "4%",
              top: "2%",
              // data: ['最高气温', '最低气温']
            },
            tooltip: {
              trigger: "axis",
            },
            toolbox: {
              show: true,
              feature: {
                dataZoom: {
                  yAxisIndex: "none",
                },
                dataView: { readOnly: false },
                magicType: { type: ["line", "bar"] },
                restore: {},
                saveAsImage: {},
              },
            },
            xAxis: {
              type: "category",
              boundaryGap: false,
              data: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"], // 用data替换这里
            },
            yAxis: {
              type: "value",
              axisLabel: {
                formatter: "{value}",
              },
            },
            series: [
              {
                name: "活跃人数",
                type: "line",
                data: response.data.objectResult.obj.dailyCount, //用data2替换这里
              },
            ],
          };
          chart.setOption(option);
          resolve(response.data.objectResult.obj.dailyCount);
        } else {
          resolve(response.data);
        }
      },
      fail(error) {
        reject(error);
        wx.showToast({
          title: "网络有点不给力哦",
          icon: "none",
        });
      },
    });
  });
}
Page({
  onLoad: function () {
    // 获取组件
    this.ecComponent = this.selectComponent('#mychart-dom-bar');
  },
  data: {
    swiperList: [
      {
        "swip_id":0,
        "swip_src":"https://ftp.bmp.ovh/imgs/2021/02/bdc601dd0da3ed18.jpg"
      },
      {
        "swip_id":1,
        "swip_src":"https://ftp.bmp.ovh/imgs/2021/02/6778ce164fb6393d.jpg"
      },
      {
        "swip_id":2,
        "swip_src":"https://ftp.bmp.ovh/imgs/2021/02/3553ae4e0fa2f865.jpg"
      }
    ],
    weekTotal:0,
    total:0,
    ec: {
      // 将 lazyLoad 设为 true 后，需要手动初始化图表
      lazyLoad: true
    },
    isLoaded: false,
    isDisposed: false
  },
  // 点击按钮后初始化图表
  init: function () {
    console.log("初始化图表");
    this.ecComponent.init((canvas, width, height, dpr) => {
      // 获取组件的 canvas、width、height 后的回调函数
      // 在这里初始化图表
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
      });
      setOption(chart);

      // 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
      this.chart = chart;

      this.setData({
        isLoaded: true,
        isDisposed: false
      });

      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return chart;
    });
  },
  onShow:function(){
    console.log("到这里");
    this.init();
  }
})